﻿using System;

using ApplicationLibrary.SqlConnector;

#region RegisterTable

namespace ApplicationManager.ModuleData
{
    public class RegisterModule : ApplicationLibrary.SqlConnector.RegisterModule
    {
        override public int CurrentVersion()
        {
            return 2;
        }

        override public string Module()
        {
            return "APPM";
        }

        override public string Application()
        {
            return "APPM";
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        override protected bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);

            #region Users Table

            if (SearchTable(AM_Users.Name))
            {
                newTable.NewTable(AM_Users.Name);
                newTable.AddColumn(AM_Users.Username);
                newTable.AddColumn(AM_Users.Password);
                newTable.AddColumn(AM_Users.Surname);
                newTable.AddColumn(AM_Users.UserType);
                newTable.AddColumn(AM_Users.Expired);
                newTable.AddColumn(AM_Users.ExpireDate);
                newTable.AddColumn(AM_Users.ChangePassword);
                newTable.AddColumn(AM_Users.Blocked);
                newTable.AddPrimaryKey(AM_Users.Username);
                newTable.Create();
            }

            #endregion

            #region Lock Table

            if (SearchTable(AM_Locks.Name))
            {
                newTable.NewTable(AM_Locks.Name);
                newTable.AddColumn(AM_Locks.ComputerName);
                newTable.AddColumn(AM_Locks.Username);
                newTable.AddColumn(AM_Locks.TableName);
                newTable.AddColumn(AM_Locks.LockKey);
                newTable.AddColumn(AM_Locks.LockDate);
                newTable.AddPrimaryKey(AM_Locks.TableName, AM_Locks.LockKey);
                newTable.Create();
            }

            #endregion

            #region AM_Version

            if (SearchTable(AM_Version.Name))
            {
                newTable.NewTable(AM_Version.Name);
                newTable.AddColumn(AM_Version.Application);
                newTable.AddColumn(AM_Version.Module);
                newTable.AddColumn(AM_Version.Version);

                newTable.AddPrimaryKey(new iColumn[] { AM_Version.Application, AM_Version.Module });
                newTable.Create();
            }

            #endregion

            //#region AM_Descriptions

            //if (SearchTable(AM_Descriptions.Name))
            //{
            //    newTable.NewTable(AM_Descriptions.Name);

            //    newTable.AddColumn(AM_Descriptions.ID);
            //    newTable.AddColumn(AM_Descriptions.Category);
            //    newTable.AddColumn(AM_Descriptions.Description);

            //    newTable.AddPrimaryKey(new iColumn[] { AM_Descriptions.ID });

            //    newTable.Create();
            //}
            //#endregion

            #region AM_Counter

            //newTable.DropTable(AM_Counter.Name);

            if (SearchTable(AM_Counter.Name))
            {
                newTable.NewTable(AM_Counter.Name);

                newTable.AddColumn(AM_Counter.Year);
                newTable.AddColumn(AM_Counter.Type);
                newTable.AddColumn(AM_Counter.Description);
                newTable.AddColumn(AM_Counter.HasPrefix);
                newTable.AddColumn(AM_Counter.PrefixRO);
                newTable.AddColumn(AM_Counter.PrefixValue);
                newTable.AddColumn(AM_Counter.PrefixMask);
                newTable.AddColumn(AM_Counter.PrefixType);
                newTable.AddColumn(AM_Counter.PrefixSep);
                newTable.AddColumn(AM_Counter.HasSuffix);
                newTable.AddColumn(AM_Counter.SuffixRO);
                newTable.AddColumn(AM_Counter.SuffixValue);
                newTable.AddColumn(AM_Counter.SuffixMask);
                newTable.AddColumn(AM_Counter.SuffixType);
                newTable.AddColumn(AM_Counter.SuffixSep);
                newTable.AddColumn(AM_Counter.CodeType);
                newTable.AddColumn(AM_Counter.CodeLen);
                newTable.AddColumn(AM_Counter.CodeKey);

                newTable.AddPrimaryKey(new iColumn[] { AM_Counter.Year, AM_Counter.Type });

                newTable.Create();
            }

            #endregion

            #region AM_CounterValue

            if (SearchTable(AM_CounterValue.Name))
            {
                newTable.NewTable(AM_CounterValue.Name);

                newTable.AddColumn(AM_CounterValue.Type);
                newTable.AddColumn(AM_CounterValue.Code);
                newTable.AddColumn(AM_CounterValue.Description);
                newTable.AddColumn(AM_CounterValue.TextValue);
                newTable.AddColumn(AM_CounterValue.NumericValue);

                newTable.AddPrimaryKey(new iColumn[] { AM_CounterValue.Type, AM_CounterValue.Code });

                newTable.Create();
            }

            #endregion

            return true;
        }

        override protected bool UpdateDBTables()
        {
            if (dbVersion < 2)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(AM_Version.Application))
                {
                    alterMaster.AlterTable(AM_Version.Name);
                    alterMaster.AddColumn(AM_Version.Application);
                    alterMaster.Alter();
                }
            }
            return true;
        }

        public override void AddScrounger(ApplicationLibrary.DBManager.iScrounger frm, ApplicationLibrary.NameSpace nameSpace)
        {
            //throw new NotImplementedException();
        }
    }
}

#endregion